"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库DDL语法
Case Name   : 源表为mot表，目标表为行存表
Create At   : 2023.08.31
Owner       : z@wan005
Description :
    1、设置enable_incremental_checkpoint=off
    2、创建mot表
    3、创建新表，源表为mot表
    4、查询新表
    5、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功，新表为普通表
    5、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest16(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_pri = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_grammar_0016"
        self.tb_name = "tb_grammar_0016"
        self.default_vales = self.common.show_param(
            'enable_incremental_checkpoint')

    def test_utc_date(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '-----step1:设置enable_incremental_checkpoint=off;' \
               'expect:成功-----'
        self.log.info(text)
        if 'off' not in self.default_vales:
            set_off = self.sh_pri.execute_gsguc(
                "set", self.constant.GSGUC_SUCCESS_MSG,
                f'enable_incremental_checkpoint=off')
            self.log.info(set_off)
            status = self.sh_pri.restart_db_cluster()
            self.log.info(status)
            res = self.common.show_param('enable_incremental_checkpoint')
            self.log.info(res)
            self.assertIn("off", res, "执行失败：" + text)

        text = '--step2:创建mot表;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
                  f"create foreign table {self.tb_name}(id int primary key," \
                  f"name varchar2(20));"
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('CREATE FOREIGN TABLE', sql_res, '执行失败' + text)

        text = '--step3:创建新表，源表为mot表;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name}_01;" \
                  f"create table if not exists {self.tb_name}_01 like " \
                  f"{self.tb_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('CREATE TABLE', sql_res, '执行失败' + text)

        text = '--step4:查询新表;expect:成功，新表为普通表--'
        self.log.info(text)
        sql_cmd = f"select relname,relkind from pg_class where " \
                  f"relname='{self.tb_name}_01';"
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(' tb_grammar_0016_01 | r', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step5:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_pri.execut_db_sql(sql_cmd,
                                              dbname='postgres')
        self.log.info(drop_res1)
        gs_guc = self.sh_pri.execute_gsguc("set",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f'enable_incremental_checkpoint'
                                           f'={self.default_vales}')
        self.log.info(gs_guc)
        status = self.sh_pri.restart_db_cluster()
        self.log.info(status)
        res = self.common.show_param('enable_incremental_checkpoint')
        self.log.info(res)
        self.assertIn("on", res, "执行失败：" + text)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
